using System;
using System.Data;
using System.Data.SqlClient;
namespace DAL
{
    /**
     * Classname : Nguoidung
     * Version information :1.0
     * Date  :26-09-2006
     * Copyright notice : 
     */

    public class DANguon : DbObject
    {
        protected SqlConnection connec;

        public DANguon(SqlConnection Connection)
        {
            connec = Connection;
        }
        // Kiem tra mat khau nguoi dung
        public DataSet LayDanhSachNguon()
        {
            // create the parameter
            DataSet dsNguon = RunProcedure("sp_Nguons_loadLayDanhSachNguon", "TB_Nguon", connec);
            for (int i = 0; i < dsNguon.Tables[0].Rows.Count; i++)
            {
                dsNguon.Tables[0].Rows[i]["Seq"] = (i + 1).ToString();
            }
            return dsNguon;
        }
		
		// return only the record with the specified ID!!!!!!!!!!!
		public DataRow loadNguonID(int NguonId)
		{
			// create the parameter
			SqlParameter[] parameters = { new SqlParameter("@NguonId", SqlDbType.Int, 4) };
			parameters[0].Value = NguonId;

            using (DataSet categorys = RunProcedure("sp_Nguons_loadNguonID", parameters, "TB_Nguon", connec))
			{
				return categorys.Tables[0].Rows[0];
			}
		}	

		// update the properties of the specified Categorys
		// Sua noi dung ban ghi hien hanh
		public int updateNguon(int NguonId, string NguonName)
		{
			int numAffected;
			// create the parameters
			SqlParameter[] parameters = { 	    new SqlParameter("@NguonId", SqlDbType.Int, 4),
											    new SqlParameter("@NguonName", SqlDbType.NVarChar, 128)
										};
			// set the values
			parameters[0].Value = NguonId;
			parameters[1].Value = NguonName.Trim();	
			// run the procedure
            RunProcedure("sp_Nguons_updateNguon", parameters, out numAffected, connec);

			return numAffected;
		}

		// add a new question
		// Them ban ghi hien hanh
		public int insertNguon(string NguonName, int LanguageID)
		{
			int numAffected;
			// create the parameters
            SqlParameter[] parameters =  {	    new SqlParameter("@NguonName", SqlDbType.NVarChar, 128),
											    new SqlParameter("@NguonId", SqlDbType.Int ,4)
										 };	
			// set the values
			parameters[0].Value = NguonName.Trim();
			parameters[1].Direction = ParameterDirection.Output;
		
			// run the procedure
            RunProcedure("sp_Nguons_insertNguon", parameters, out numAffected, connec);

			return (int)parameters[1].Value;
		}
		// Xoa Role
		public int removeNguon(int NguonID)
		{
			int numAffected;
			// create the parameters
			SqlParameter[] parameters = {	new SqlParameter("@NguonID", SqlDbType.Int)	};
			parameters[0].Value = NguonID;			
			// run the procedure
            try
            {
                RunProcedure("sp_Nguons_removeNguons", parameters, out numAffected, connec);
            }
            catch
            {
                numAffected = 0;
            }
			return numAffected;			
		}
    }
}